package com.ibeeking.found.cop.b.rest.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ibeeking.found.common.enums.EmployeeStatusEnum;
import com.ibeeking.found.common.enums.SexEnum;
import com.ibeeking.found.common.utils.UrlUtils;
import com.ibeeking.found.common.utils.UserUtils;
import com.ibeeking.found.cop.api.common.dto.OssPolicyDto;
import com.ibeeking.found.cop.api.feign.FileFeignClient;
import com.ibeeking.found.cop.b.rest.service.ICopDepartmentService;
import com.ibeeking.found.cop.b.rest.service.ICopEmployeeService;
import com.ibeeking.found.cop.b.rest.service.IMqService;
import com.ibeeking.found.cop.service.common.dos.CopDepartmentDO;
import com.ibeeking.found.cop.service.common.dos.CopDepartmentEmployeeDO;
import com.ibeeking.found.cop.service.common.dos.CopEmployeeDO;
import com.ibeeking.found.cop.service.common.enums.MqSyncOperatorTypeEnum;
import com.ibeeking.found.cop.service.common.param.CopDepartmentEmployeeParam;
import com.ibeeking.found.cop.service.common.param.CopEmployeeParam;
import com.ibeeking.found.cop.service.common.param.MqSyncCopParam;
import com.ibeeking.found.cop.service.common.query.CopEmployeePageQuery;
import com.ibeeking.found.cop.service.common.query.CopEmployeeQuery;
import com.ibeeking.found.cop.service.common.vo.CopDepartmentVO;
import com.ibeeking.found.cop.service.common.vo.CopEmployeeVO;
import com.ibeeking.found.cop.service.mapper.CopDepartmentEmployeeMapper;
import com.ibeeking.found.cop.service.mapper.CopDepartmentMapper;
import com.ibeeking.found.cop.service.mapper.CopEmployeeMapper;
import com.ibeeking.nematos.exception.BusinessException;
import com.ibeeking.nematos.log.utils.LogUtils;
import com.ibeeking.nematos.utils.bean.BeanUtil;
import com.ibeeking.nematos.utils.data.Collections3;
import com.ibeeking.nematos.utils.data.TreeFastUtils;
import com.ibeeking.nematos.utils.file.FileUtils;
import com.ibeeking.nematos.utils.id.IdGenerator;
import com.ibeeking.nematos.utils.json.JsonUtils;
import com.ibeeking.nematos.utils.result.ResponseResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;


/**
 * @ClassName CopDepartmentServiceImpl
 * @Description
 * @Author ibeeking
 * @Date 2021-01-27 18:50
 **/
@Service
public class CopEmployeeServiceImpl extends ServiceImpl<CopEmployeeMapper, CopEmployeeDO> implements ICopEmployeeService {

    @Resource
    private CopEmployeeMapper copEmployeeMapper;

    @Resource
    private CopDepartmentMapper copDepartmentMapper;

    @Resource
    private CopDepartmentEmployeeMapper copDepartmentEmployeeMapper;

    @Resource
    private ICopDepartmentService copDepartmentServiceImpl;

    @Resource
    private IMqService mqServiceImpl;

    @Resource
    private FileFeignClient fileFeignClient;

    @Override
    public List<CopEmployeeVO> list(CopEmployeeQuery query){
        List<CopDepartmentDO> copDepartmentDOS = copDepartmentMapper.selectList(new LambdaQueryWrapper<CopDepartmentDO>());
        Long mainDepartmentId = query.getMainDepartmentId();
        List<Long> allMainDepartmentIds = new ArrayList<>();
        if(mainDepartmentId!=null){
            allMainDepartmentIds.add(mainDepartmentId);
           if(Collections3.isNotEmpty(copDepartmentDOS)){
               List<CopDepartmentVO> allCopDepartmentTrees = JsonUtils.jsonToList(copDepartmentDOS, CopDepartmentVO.class);
               List<Long> mainDepartmentIds= new ArrayList<>();
               mainDepartmentIds.add(mainDepartmentId);
               List<Long> result = new ArrayList<>();
               TreeFastUtils.selectLongCidByPid(result, allCopDepartmentTrees, mainDepartmentIds);
               allMainDepartmentIds.addAll(result);
           }
        }
        LogUtils.info("==========allMainDepartmentIds:{}=========" , JsonUtils.toJsonStr(allMainDepartmentIds));
        LambdaQueryWrapper<CopEmployeeDO> queryWrapper = new LambdaQueryWrapper<>(CopEmployeeDO.class)
                .eq(query.getId()!= null, CopEmployeeDO::getId, query.getId())
                .eq(StringUtils.isNotEmpty(query.getEmployeeCode()), CopEmployeeDO::getEmployeeCode, query.getEmployeeCode())
                .eq(query.getGender() != null, CopEmployeeDO::getGender, query.getGender())
                .eq(query.getStatus() != null, CopEmployeeDO::getStatus, query.getStatus())
                .in(Collections3.isNotEmpty(allMainDepartmentIds), CopEmployeeDO::getMainDepartmentId, allMainDepartmentIds)
                .like(StringUtils.isNotEmpty(query.getEmployeeName()), CopEmployeeDO::getEmployeeName, query.getEmployeeName())
                .like(StringUtils.isNotEmpty(query.getNickName()), CopEmployeeDO::getNickName, query.getNickName())
                .orderByDesc(CopEmployeeDO::getCreateTime);
        List<CopEmployeeDO> copEmployeeDOS = copEmployeeMapper.selectList(queryWrapper);

        Map<Long, String> copDepartmentNameCnMaps = Collections3.isNotEmpty(copDepartmentDOS)?copDepartmentDOS.parallelStream().collect(Collectors.toMap(CopDepartmentDO::getId, CopDepartmentDO::getName, (o, n) -> o)):null;

        List<CopEmployeeVO> copEmployeeVOS = copEmployeeDOS.stream().map(d->{
            CopEmployeeVO copEmployeeVO = BeanUtil.convertBean(d, CopEmployeeVO.class);
            copEmployeeVO.setGenderDesc(SexEnum.getDescByCode(d.getGender()));
            copEmployeeVO.setMainDepartmentName(copDepartmentNameCnMaps!=null?copDepartmentNameCnMaps.get(d.getMainDepartmentId()):null);

            if(StringUtils.isNotEmpty(d.getNickName())){
                String employeeNameAndNick = (d.getEmployeeName()).concat("(").concat(d.getNickName()).concat(")");
                copEmployeeVO.setEmployeeNameAndNick(employeeNameAndNick);
            }else{
                copEmployeeVO.setEmployeeNameAndNick(d.getEmployeeName());
            }
            copEmployeeVO.setStatusDesc(EmployeeStatusEnum.getDescByCode(d.getStatus()));
            return copEmployeeVO;
        }).collect(Collectors.toList());
        return copEmployeeVOS;
    }

    @Override
    public Page<CopEmployeeVO> pageList(CopEmployeePageQuery pageQuery){
        List<CopDepartmentDO> copDepartmentDOS = copDepartmentMapper.selectList(new LambdaQueryWrapper<CopDepartmentDO>());
        Long mainDepartmentId = pageQuery.getMainDepartmentId();
        List<Long> allMainDepartmentIds = new ArrayList<>();
        if(mainDepartmentId!=null){
            allMainDepartmentIds.add(mainDepartmentId);
            if(Collections3.isNotEmpty(copDepartmentDOS)){
                List<CopDepartmentVO> allCopDepartmentTrees = JsonUtils.jsonToList(copDepartmentDOS, CopDepartmentVO.class);
                List<Long> mainDepartmentIds= new ArrayList<>();
                mainDepartmentIds.add(mainDepartmentId);
                List<Long> result = new ArrayList<>();
                TreeFastUtils.selectLongCidByPid(result, allCopDepartmentTrees, mainDepartmentIds);
                allMainDepartmentIds.addAll(result);
            }
        }
        LogUtils.info("==========allMainDepartmentIds:{}=========" , JsonUtils.toJsonStr(allMainDepartmentIds));
        LambdaQueryWrapper<CopEmployeeDO> queryWrapper = new LambdaQueryWrapper<>(CopEmployeeDO.class)
                .eq(pageQuery.getId()!= null, CopEmployeeDO::getId, pageQuery.getId())
                .eq(StringUtils.isNotEmpty(pageQuery.getEmployeeCode()), CopEmployeeDO::getEmployeeCode, pageQuery.getEmployeeCode())
                .eq(pageQuery.getGender() != null, CopEmployeeDO::getGender, pageQuery.getGender())
                .eq(pageQuery.getStatus() != null, CopEmployeeDO::getStatus, pageQuery.getStatus())
                .in(Collections3.isNotEmpty(allMainDepartmentIds), CopEmployeeDO::getMainDepartmentId, allMainDepartmentIds)
                .like(StringUtils.isNotEmpty(pageQuery.getEmployeeName()), CopEmployeeDO::getEmployeeName, pageQuery.getEmployeeName())
                .like(StringUtils.isNotEmpty(pageQuery.getNickName()), CopEmployeeDO::getNickName, pageQuery.getNickName())
                .orderByDesc(CopEmployeeDO::getCreateTime);
        Page<CopEmployeeDO> copEmployeeDOPages = copEmployeeMapper.selectPage(new Page<CopEmployeeDO>(pageQuery.getPageNum(), pageQuery.getPageSize()), queryWrapper);
        List<CopEmployeeDO> copEmployeeDOS = copEmployeeDOPages.getRecords();
        Page<CopEmployeeVO> copEmployeeVOPages = BeanUtil.convertPage(copEmployeeDOPages, CopEmployeeVO.class);

        Map<Long, String> copDepartmentNameCnMaps = Collections3.isNotEmpty(copDepartmentDOS)?copDepartmentDOS.parallelStream().collect(Collectors.toMap(CopDepartmentDO::getId, CopDepartmentDO::getName, (o, n) -> o)):null;

        List<CopEmployeeVO> copEmployeeVOS = copEmployeeDOS.stream().map(d->{
            CopEmployeeVO copEmployeeVO = BeanUtil.convertBean(d, CopEmployeeVO.class);
            copEmployeeVO.setGenderDesc(SexEnum.getDescByCode(d.getGender()));
            copEmployeeVO.setMainDepartmentName(copDepartmentNameCnMaps!=null?copDepartmentNameCnMaps.get(d.getMainDepartmentId()):null);

            if(StringUtils.isNotEmpty(d.getNickName())){
                String employeeNameAndNick = (d.getEmployeeName()).concat("(").concat(d.getNickName()).concat(")");
                copEmployeeVO.setEmployeeNameAndNick(employeeNameAndNick);
            }else{
                copEmployeeVO.setEmployeeNameAndNick(d.getEmployeeName());
            }
            copEmployeeVO.setStatusDesc(EmployeeStatusEnum.getDescByCode(d.getStatus()));
            return copEmployeeVO;
        }).collect(Collectors.toList());
        copEmployeeVOPages.setRecords(copEmployeeVOS);
        return copEmployeeVOPages;
    }

    @Override
    public Page<CopEmployeeVO> pageListWithSubDepartment(CopEmployeePageQuery pageQuery){
        if(pageQuery.getMainDepartmentId() == null){
            throw new BusinessException("部门ID不能为空");
        }
        List<Long> mainDepartmentIds = copDepartmentServiceImpl.getDepartmentIdWithSubByParentId(pageQuery.getMainDepartmentId());
        LogUtils.info("=======mainDepartmentIds:{}=========",JsonUtils.toJsonStr(mainDepartmentIds));
        LambdaQueryWrapper<CopEmployeeDO> queryWrapper = new LambdaQueryWrapper<>(CopEmployeeDO.class)
                .eq(pageQuery.getId()!= null, CopEmployeeDO::getId, pageQuery.getId())
                .eq(StringUtils.isNotEmpty(pageQuery.getEmployeeCode()), CopEmployeeDO::getEmployeeCode, pageQuery.getEmployeeCode())
                .eq(pageQuery.getGender() != null, CopEmployeeDO::getGender, pageQuery.getGender())
                .eq(pageQuery.getStatus() != null, CopEmployeeDO::getStatus, pageQuery.getStatus())
                .in(Collections3.isNotEmpty(mainDepartmentIds), CopEmployeeDO::getMainDepartmentId, mainDepartmentIds)
                .like(StringUtils.isNotEmpty(pageQuery.getEmployeeName()), CopEmployeeDO::getEmployeeName, pageQuery.getEmployeeName())
                .like(StringUtils.isNotEmpty(pageQuery.getNickName()), CopEmployeeDO::getNickName, pageQuery.getNickName())
                .orderByDesc(CopEmployeeDO::getCreateTime);
        Page<CopEmployeeDO> copEmployeeDOPages = copEmployeeMapper.selectPage(new Page<CopEmployeeDO>(pageQuery.getPageNum(), pageQuery.getPageSize()), queryWrapper);
        List<CopEmployeeDO> copEmployeeDOS = copEmployeeDOPages.getRecords();
        Page<CopEmployeeVO> copEmployeeVOPages = BeanUtil.convertPage(copEmployeeDOPages, CopEmployeeVO.class);

        List<CopDepartmentDO> copDepartmentDOS = copDepartmentMapper.selectList(new LambdaQueryWrapper<CopDepartmentDO>());
        Map<Long, String> copDepartmentNameCnMaps = copDepartmentDOS.parallelStream().collect(Collectors.toMap(CopDepartmentDO::getId, CopDepartmentDO::getName, (o, n) -> o));
        //Map<Long, String> copDepartmentNameEnMaps = copDepartmentDOS.parallelStream().collect(Collectors.toMap(CopDepartmentDO::getId, CopDepartmentDO::getNameEn, (o, n) -> o));

        List<CopEmployeeVO> copEmployeeVOS = copEmployeeDOS.stream().map(d->{
            CopEmployeeVO copEmployeeVO = BeanUtil.convertBean(d, CopEmployeeVO.class);
            copEmployeeVO.setGenderDesc(SexEnum.getDescByCode(d.getGender()));
            copEmployeeVO.setMainDepartmentName(copDepartmentNameCnMaps.get(d.getMainDepartmentId()));
            //copEmployeeVO.setMainDepartmentNameEn(copDepartmentNameEnMaps.get(d.getMainDepartmentId()));
            if(StringUtils.isNotEmpty(d.getNickName())){
                String employeeNameAndNick = (d.getEmployeeName()).concat("(").concat(d.getNickName()).concat(")");
                copEmployeeVO.setEmployeeNameAndNick(employeeNameAndNick);
            }else{
                copEmployeeVO.setEmployeeNameAndNick(d.getEmployeeName());
            }
            copEmployeeVO.setStatusDesc(EmployeeStatusEnum.getDescByCode(d.getStatus()));
            return copEmployeeVO;
        }).collect(Collectors.toList());
        copEmployeeVOPages.setRecords(copEmployeeVOS);
        return copEmployeeVOPages;
    }

    @Override
    public List<CopEmployeeVO> listByDepartmentId(Long departmentId){

        LambdaQueryWrapper<CopEmployeeDO> queryWrapper = new LambdaQueryWrapper<>(CopEmployeeDO.class);
        queryWrapper.eq(CopEmployeeDO::getMainDepartmentId, departmentId);

        List<CopEmployeeDO> copEmployeeDOS = copEmployeeMapper.selectList(queryWrapper);

        List<CopDepartmentDO> copDepartmentDOS = copDepartmentMapper.selectList(new LambdaQueryWrapper<CopDepartmentDO>());
        Map<Long, String> copDepartmentNameCnMaps = copDepartmentDOS.parallelStream().collect(Collectors.toMap(CopDepartmentDO::getId, CopDepartmentDO::getName, (o, n) -> o));
        //Map<Long, String> copDepartmentNameEnMaps = copDepartmentDOS.parallelStream().collect(Collectors.toMap(CopDepartmentDO::getId, CopDepartmentDO::getNameEn, (o, n) -> o));

        List<CopEmployeeVO> copEmployeeVOS = copEmployeeDOS.stream().map(d->{
            CopEmployeeVO copEmployeeVO = BeanUtil.convertBean(d, CopEmployeeVO.class);
            copEmployeeVO.setGenderDesc(SexEnum.getDescByCode(d.getGender()));
            copEmployeeVO.setMainDepartmentName(copDepartmentNameCnMaps.get(d.getMainDepartmentId()));
            //copEmployeeVO.setMainDepartmentNameEn(copDepartmentNameEnMaps.get(d.getMainDepartmentId()));
            if(StringUtils.isNotEmpty(d.getNickName())){
                String employeeNameAndNick = (d.getEmployeeName()).concat("(").concat(d.getNickName()).concat(")");
                copEmployeeVO.setEmployeeNameAndNick(employeeNameAndNick);
            }else{
                copEmployeeVO.setEmployeeNameAndNick(d.getEmployeeName());
            }
            copEmployeeVO.setStatusDesc(EmployeeStatusEnum.getDescByCode(d.getStatus()));
            return copEmployeeVO;
        }).collect(Collectors.toList());
        return copEmployeeVOS;
    }

    @Override
    public CopEmployeeVO view(Long id){
        CopEmployeeDO copEmployeeDO = copEmployeeMapper.selectById(id);
        if(copEmployeeDO == null) return null;

        List<CopDepartmentDO> copDepartmentDOS = copDepartmentMapper.selectList(new LambdaQueryWrapper<CopDepartmentDO>());
        Map<Long, String> copDepartmentNameCnMaps = copDepartmentDOS.parallelStream().collect(Collectors.toMap(CopDepartmentDO::getId, CopDepartmentDO::getName, (o, n) -> o));
        //Map<Long, String> copDepartmentNameEnMaps = copDepartmentDOS.parallelStream().collect(Collectors.toMap(CopDepartmentDO::getId, CopDepartmentDO::getNameEn, (o, n) -> o));

        CopEmployeeVO copEmployeeVO = BeanUtil.convertBean(copEmployeeDO, CopEmployeeVO.class);
        copEmployeeVO.setGenderDesc(SexEnum.getDescByCode(copEmployeeVO.getGender()));
        copEmployeeVO.setMainDepartmentName(copDepartmentNameCnMaps.get(copEmployeeVO.getMainDepartmentId()));
        //copEmployeeVO.setMainDepartmentNameEn(copDepartmentNameEnMaps.get(copEmployeeVO.getMainDepartmentId()));
        if(StringUtils.isNotEmpty(copEmployeeVO.getNickName())){
            String employeeNameAndNick = (copEmployeeVO.getEmployeeName()).concat("(").concat(copEmployeeVO.getNickName()).concat(")");
            copEmployeeVO.setEmployeeNameAndNick(employeeNameAndNick);
        }else{
            copEmployeeVO.setEmployeeNameAndNick(copEmployeeVO.getEmployeeName());
        }
        copEmployeeVO.setStatusDesc(EmployeeStatusEnum.getDescByCode(copEmployeeVO.getStatus()));

        return copEmployeeVO;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer syncEmployeeFromWx(Long departmentId) {
        String tenantId = UserUtils.getTenantId();
        MqSyncCopParam mqParam = new MqSyncCopParam();
        mqParam.setOperType(MqSyncOperatorTypeEnum.COP_SYNC_EMPLOYEE_LIST.getCode());
        mqParam.setTenantId(Long.valueOf(tenantId));
        mqParam.setMainDepartmentId(departmentId);
        mqServiceImpl.sendSyncToWxTaskMessage(mqParam);
        return 1;
    }

    @Override
    public Integer batchInsertEmployeeListFromWxCp(List<CopEmployeeParam> params) {
        if(Collections3.isNotEmpty(params)){
            List<CopEmployeeDO> copEmployeeDOS = new ArrayList<>();
            CopEmployeeParam copEmployeeParam = params.get(0);
            Long tenantId = copEmployeeParam.getTenantId();
            copEmployeeDOS = params.stream().map(d->{
                CopEmployeeDO copEmployeeDO = BeanUtil.convertBean(d, CopEmployeeDO.class);
                copEmployeeDO.setCreateTime(LocalDateTime.now());
                copEmployeeDO.setModifyTime(LocalDateTime.now());
                return copEmployeeDO;
            }).collect(Collectors.toList());
            return copEmployeeMapper.batchInsertEmployeeListFromWxCp(tenantId, copEmployeeDOS);
        }
        return 0;
    }

    @Override
    public Integer insertEmployeeFromWxCp(CopEmployeeParam param) {
        if(null != param){
            Long tenantId = param.getTenantId();
            CopEmployeeDO copEmployeeDO = BeanUtil.convertBean(param, CopEmployeeDO.class);
            copEmployeeDO.setCreateTime(LocalDateTime.now());
            copEmployeeDO.setModifyTime(LocalDateTime.now());
            return copEmployeeMapper.insertEmployeeFromWxCp(tenantId, copEmployeeDO);
        }
        return 0;
    }

    @Override
    public Integer updateEmployeeFromWxCp(CopEmployeeParam param) {
        if(null != param){
            Long tenantId = param.getTenantId();
            CopEmployeeDO copEmployeeDO = BeanUtil.convertBean(param, CopEmployeeDO.class);
            copEmployeeDO.setCreateTime(LocalDateTime.now());
            copEmployeeDO.setModifyTime(LocalDateTime.now());
            return copEmployeeMapper.updateEmployeeFromWxCp(tenantId, copEmployeeDO);
        }
        return 1;
    }

    @Override
    public Integer deleteEmployeeFromWxCp(Long tenantId, Long id) {
        return copEmployeeMapper.deleteEmployeeFromWxCp(tenantId, id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer saveOrUpdateOrDeleteBatchEmployee(Long tenantId, List<CopEmployeeParam> params){
        if(Collections3.isNotEmpty(params)) {
            List<Long> employeeIds = new ArrayList<>();
            List<CopEmployeeDO> needInsertCopEmployeeDOS = new ArrayList<>();
            List<CopEmployeeDO> needUpdateCopEmployeeDOS = new ArrayList<>();
            List<CopEmployeeDO> needDeleteCopEmployeeDOS = new ArrayList<>();
            Map<String, Long> employeeCodeAndIdMap = null;
            List<CopEmployeeDO> oldCopEmployeeDOS = copEmployeeMapper.selectList(new LambdaQueryWrapper<>());
            if(Collections3.isNotEmpty(oldCopEmployeeDOS)) {
                employeeCodeAndIdMap = oldCopEmployeeDOS.parallelStream().collect(Collectors.toMap(CopEmployeeDO::getEmployeeCode, CopEmployeeDO::getId, (o, n) -> o));
                List<String> oldEmployeeCodeList = oldCopEmployeeDOS.parallelStream().map(CopEmployeeDO::getEmployeeCode).collect(Collectors.toList());
                List<String> newEmployeeCodeList = params.parallelStream().map(CopEmployeeParam::getEmployeeCode).collect(Collectors.toList());
                if (Collections3.isNotEmpty(oldEmployeeCodeList) && Collections3.isNotEmpty(newEmployeeCodeList)) {
                    HashSet<String> oldEmployeeCodeHash = new HashSet<>(oldEmployeeCodeList);
                    HashSet<String> newEmployeeCodeHash = new HashSet<>(newEmployeeCodeList);
                    oldEmployeeCodeHash.removeAll(newEmployeeCodeHash);
                    if(oldEmployeeCodeHash.size()>0) {
                        List<String> needDeleteCopEmployeeCodes = new ArrayList<>(oldEmployeeCodeHash);
                        needDeleteCopEmployeeDOS = copEmployeeMapper.selectList(new LambdaQueryWrapper<CopEmployeeDO>().in(CopEmployeeDO::getEmployeeCode,needDeleteCopEmployeeCodes));
                    }
                }
            }

            List<CopEmployeeDO> copEmployeeDOS = new ArrayList<>();
            List<CopDepartmentEmployeeDO> copDepartmentEmployeeDOS = new ArrayList<>();
            OssPolicyDto ossPolicyDto = getOssFilePolicy();
            for(CopEmployeeParam d: params){
                CopEmployeeDO copEmployeeDO = BeanUtil.convertBean(d, CopEmployeeDO.class);
                Long employeeId = d.getId();
                copEmployeeDO.setCreateBy(UserUtils.getUserId());
                copEmployeeDO.setCreateTime(LocalDateTime.now());
                copEmployeeDO.setModifyTime(LocalDateTime.now());
                if(StringUtils.isNotEmpty(d.getAvatar())) {
                    String avatar = transferFileToOss(d.getEmployeeCode() + "_avatar", d.getAvatar(), ossPolicyDto);
                    copEmployeeDO.setAvatar(avatar);
                }
                if(StringUtils.isNotEmpty(d.getThumbAvatar())) {
                    String thumbAvatar = transferFileToOss(d.getEmployeeCode() + "_thumb_avatar", d.getThumbAvatar(), ossPolicyDto);
                    copEmployeeDO.setThumbAvatar(thumbAvatar);
                }
                if(StringUtils.isNotEmpty(d.getQrCode())) {
                    String qrCode = transferFileToOss(d.getEmployeeCode() + "_qrcode", d.getQrCode(), ossPolicyDto);
                    copEmployeeDO.setQrCode(qrCode);
                }
                if(employeeCodeAndIdMap != null && employeeCodeAndIdMap.containsKey(d.getEmployeeCode())){
                    needUpdateCopEmployeeDOS.add(copEmployeeDO);
                    employeeId = employeeCodeAndIdMap.get(d.getEmployeeCode());
                    copEmployeeDO.setId(employeeId);
                    employeeIds.add(employeeId);
                }else{
                    employeeId = IdGenerator.getId();
                    copEmployeeDO.setId(employeeId);
                    needInsertCopEmployeeDOS.add(copEmployeeDO);
                    employeeIds.add(employeeId);
                }
                List<CopDepartmentEmployeeParam> departmentEmployeeParams = d.getDepartmentEmployeeParam();
                if(Collections3.isNotEmpty(departmentEmployeeParams)){
                    for(CopDepartmentEmployeeParam empParam: departmentEmployeeParams){
                        CopDepartmentEmployeeDO copDepartmentEmployeeDO = BeanUtil.convertBean(empParam, CopDepartmentEmployeeDO.class);
                        copDepartmentEmployeeDO.setId(IdGenerator.getId());
                        copDepartmentEmployeeDO.setEmployeeId(employeeId);
                        copDepartmentEmployeeDOS.add(copDepartmentEmployeeDO);
                    }
                }

            };
            if(Collections3.isNotEmpty(needDeleteCopEmployeeDOS)){
                List<Long> delIds = needDeleteCopEmployeeDOS.stream().map(CopEmployeeDO::getId).collect(Collectors.toList());
                copEmployeeMapper.deleteBatchIds(delIds);
            }

            if(Collections3.isNotEmpty(needInsertCopEmployeeDOS)){
                copEmployeeMapper.insertBatchSomeColumn(needInsertCopEmployeeDOS);
            }
            if(Collections3.isNotEmpty(needUpdateCopEmployeeDOS)){
                super.updateBatchById(needUpdateCopEmployeeDOS);
            }

            if(Collections3.isNotEmpty(copDepartmentEmployeeDOS)){
                copDepartmentEmployeeMapper.deleteByEmployeeId(tenantId, new HashSet<>(employeeIds));
                copDepartmentEmployeeMapper.insertBatchSomeColumn(copDepartmentEmployeeDOS);
            }
        }
        return 1;
    }

    private String transferFileToOss(String employeeId, String fileUrl, OssPolicyDto policy){
        try {
            String fileName = (String.valueOf(System.currentTimeMillis())).concat("_".concat(employeeId).concat(".png"));
            String fileTag = policy.getDir() + fileName;
            MultipartFile multipartFile = FileUtils.getMultipartFile(fileUrl, fileName, fileTag);
            ResponseResult<String> responseResult = fileFeignClient.uploadFile(multipartFile);
            String fileUploadPath = responseResult.getData();
            return UrlUtils.removeUrlDomainName(fileUploadPath);
        } catch (Exception ex) {
            throw new BusinessException("上传头像文件失败：" + ex.toString());
        }
    }

    private OssPolicyDto getOssFilePolicy(){
        ResponseResult<OssPolicyDto> responseResult = fileFeignClient.policy();
        return responseResult.getData();
    }

}
